<?php
class Delivery_Model_Mapper_Clientes
	extends Delivery_Model_Mapper_Abstract
{
	
	public function getDbTable()
	{
		return parent::getDbTable( 'Clientes' );
	}
	
	public function save( $cliente ){
		
		$dbTable = $this->getDbTAble();
		
		if ( $cliente->getId() == null ){
		   
			$cliente->cantidadPedidos = 0;
			
			if ( $cliente->getFacebookId() == null ){
			
				$cliente->generarCodigo();	
				$cliente->activo = 0;
				$cliente->setClave( $cliente->clave );
			
			}
			
			$cliente->idCliente = $dbTable->insert( $cliente );
			
			return $cliente->idCliente;
		
		} else {
			$dbTable->update( $cliente );
		}
	}
	
	/**
	 * @param int idCliente
	 * @return object Delivery_Model_Cliente
	 */
	public function getById( $idCliente ){
	
		if ( $this->getIdentityMap()->isRegistered( 'Delivery_Model_Cliente', $idCliente )){
			return $this->getIdentityMap()->get( 'Delivery_Model_Cliente', $idCliente );
		}
		
		$dbTable= $this->getDbTAble();
		$row	= $dbTable->getById( $idCliente );
	
		if( empty($row) ){
			throw new Delivery_Model_Exception( Delivery_Model_Exception::USER_NOT_FOUND );
		}
		
		return $this->_fetch( $row );
	}
	
	public function getByIds( $ids )
	{
		$dbTable= $this->getDbTAble();
		
		return $this->_fetchCollection( 
			'Clientes',
			$dbTable->getByIds( $ids )
		);
	}
	
	
	/**
	 * @param string $email
	 * @return object Delivery_Model_Cliente
	 */
	public function getByEmail( $email ){
	
		$dbTable= $this->getDbTAble();
		Delivery_Model_DbTable_Desactivable::onlyActive( false );
		$row = $dbTable->getByEmail( $email );
		Delivery_Model_DbTable_Desactivable::onlyActive();
		
		if( empty($row) ){
			throw new Delivery_Model_Exception( Delivery_Model_Exception::USER_NOT_FOUND );
		}
		
		return $this->_fetch( $row );
	}
	
	/**
	 * @param string $email
	 * @return bool
	 */
	public function exists( $email ){
	
		$dbTable= $this->getDbTAble();
		$row	= $dbTable->getByEmail( $email );
		
		return !empty($row);
	}
	
	/**
	 * @param string $email
	 * @param string $clave
	 * @return object Delivery_Model_Cliente
	 */
	public function login ( $email, $clave){
	
		$dbTable= $this->getDbTAble();
		
		$clave = Delivery_Model_Cliente::clave( $clave );
		$row = $dbTable->getByAccount( $email, $clave );
		if( empty($row) ){
			throw new Delivery_Model_Exception( Delivery_Model_Exception::USER_LOGIN_FAILED );
		}
		
		return $this->_fetch( $row );
	}
	
	/**
	 * @param int $fbUserId
	 * @return object Delivery_Model_Cliente
	 */
	public function loginFB ( $fbUserId ){
	
		$dbTable= $this->getDbTAble();
		
		$row = $dbTable->getByFacebookId( $fbUserId );
		
		return $row ? $this->_fetch( $row ) : null ;
	} 
	
	public function loadDirecciones( $cliente )
	{
		$mapper = Delivery_Model_Factory::mapper( 'DireccionesCliente' );
		$direcciones = $mapper->getByCliente( $cliente->getId() );
		
		$cliente->setDirecciones( $direcciones );
	}
	
	
	/**
	 * @param array
	 * @return objeto Delivery_Model_Cliente
	 */
	protected function _fetch( $row )
	{
		$cliente = new Delivery_Model_Cliente( $row );
		
		$this->getIdentityMap()->set( $cliente, $cliente->getId() );
		
		return $cliente;
	}
	
}